package com.foton.limiter.construstlimiter.base.config;

import com.foton.limiter.construstlimiter.base.obser.TransObser;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author RenPu
 * @Date 2023/9/4 16:48
 * @Version 1.0
 * @Description:
 **/

@Service
public class TransObserManager implements InitializingBean {

    @Autowired
    private ApplicationContext applicationContext;

    private final List<TransObser> obserList=new ArrayList<>();

    private final ExecutorService executorService=Executors.newFixedThreadPool(10);

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, TransObser> beans = applicationContext.getBeansOfType(TransObser.class);
        beans.values().forEach(this::add);
    }


    public void notifyObser(String content){
        obserList.forEach(obserList->{
            executorService.execute(()->{
                obserList.action(content);
            });
        });

    }



    public void add(TransObser transObser){
        obserList.add(transObser);
    }


}
